using System;
using System.Web;
using System.IO;
using System.Web.Security;
using WindowsLive;
using Microsoft.SharePoint;

namespace CKS.WindowsLiveAuthentication
{
    /// <summary>
    /// This page handles the login, logout and clearcookie Web Auth
    /// actions.  When you create a Windows Live application, you must
    /// specify the URL of this handler page.
    /// </summary>
    public partial class liveauthhandler : System.Web.UI.Page
    {
        protected string RedirUrl = "/default.aspx";
        protected BlogUserProfile bup;
        protected WindowsLiveLogin.User user;

        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
    #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            user = wll.ProcessLogin(Request.Form);

            if (user != null)
            {
                bup = BlogUserProfile.GetUser(user.Id);
            }
            else
            {
                FormsAuthentication.SignOut();
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {

            if (Request.QueryString["action"] == "logout")
            {
                FormsAuthentication.SignOut();
            }
            else if (Request.QueryString["action"] == "login")
            {
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    RedirUrl = AuthConfig.LiveLoginUrl + Request.QueryString["ReturnUrl"];
                }
                else
                {
                    RedirUrl = AuthConfig.LiveLoginUrl + Request.UrlReferrer;
                }
            }
            else
            {
                if (bup == null)
                {
                    BlogUserProfile.CreateUser(user.Id, "");
                    bup = BlogUserProfile.GetUser(user.Id);
                }

                if (!bup.Locked)
                {
                    FormsAuthentication.SetAuthCookie(user.Id, user.UsePersistentCookie);
                    bup.PushProfile();

                    if (bup.Email == string.Empty)
                    {
                        RedirUrl = string.Format("/_layouts/liveinfo.aspx?Source={0}", user.Context);
                    }
                    else
                    {
                        if (user != null && user.Context.Length > 0)
                        {
                            RedirUrl = user.Context;
                        }
                    }
                }
                else
                {
                    FormsAuthentication.SignOut();
                    RedirUrl = string.Format(AuthConfig.LockedUrl);
                }
            }

            Response.Redirect(RedirUrl);
        }
    }
}
